package ntut.andengine.physics;

import ntut.andengine.game.GameLevel;
import ntut.andengine.gamedesign.GameSceneLevel;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Filter;
import com.badlogic.gdx.physics.box2d.Fixture;

public class PhysicalComponent {
	
	public enum ComponentColor{
		White,
        Red,
        Blue
	}
	
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	protected Body mBody;
	protected BitmapTexture mBitmapTexture;	
	protected Sprite mSprite;
	protected ComponentColor mColor;
	
	// ===========================================================
	// Constructors
	// ===========================================================
	
	public PhysicalComponent(GameLevel level) {
		mColor = ComponentColor.White;
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public Body getBody(){
		return mBody;
	}
	
	public Vector2 getPosition() {
		Vector2 position = new Vector2(mBody.getPosition().x*32f, mBody.getPosition().y*32f);
		return position;
	}

	public void setPosition(Vector2 position) {
		mBody.setTransform(position.x/32f,position.y/32f, 0); 
	}

	public Vector2 getVelocity() {
		return mBody.getLinearVelocity();
	}

	public void setVelocity(Vector2 velocity) {
		mBody.setLinearVelocity(velocity);
	}

	public ComponentColor getColor() {
		return mColor;
	}

	public void setColor(ComponentColor color) {	
		
		mColor = color;	
		Filter filter = new Filter();
		
		switch(color){
		case White:
			if(mSprite != null) mSprite.setColor(1, 1, 1);
			filter.categoryBits = GameSceneLevel.CATEGORYBIT_WHITE;
			filter.maskBits = GameSceneLevel.MASKBITS_WHITE;			
			for (Fixture f:mBody.getFixtureList()){
				f.setFilterData(filter);
			}
			break;
		case Red:
			mSprite.setColor(1, 0, 0);
			filter.categoryBits = GameSceneLevel.CATEGORYBIT_RED;
			filter.maskBits = GameSceneLevel.MASKBITS_RED;			
			for (Fixture f:mBody.getFixtureList()){
				f.setFilterData(filter);
			}
			break;
		case Blue:
			mSprite.setColor(0, 0, 1);	
			filter.categoryBits = GameSceneLevel.CATEGORYBIT_BLUE;
			filter.maskBits = GameSceneLevel.MASKBITS_BLUE;			
			for (Fixture f:mBody.getFixtureList()){
				f.setFilterData(filter);
			}
			break;
		}
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================

	public void changeToRed(){
		
		if (mSprite == null) return;

        if (mColor == ComponentColor.Red) mSprite.setColor(1, 0, 0, 1);
        else if (mColor == ComponentColor.Blue) mSprite.setColor(1f, 1f, 1f, 0.1f);
        else if (mColor == ComponentColor.White) mSprite.setColor(1, 1, 1, 1);
	}
	
	public void changeToBlue(){
		
		if (mSprite == null) return;

        if (mColor == ComponentColor.Red) mSprite.setColor(1f, 1f, 1f, 0.1f);
        else if (mColor == ComponentColor.Blue) mSprite.setColor(0, 0, 1, 1);
        else if (mColor == ComponentColor.White) mSprite.setColor(1, 1, 1, 1);
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	
}
